//
//  WNTabLastView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/31.
//

import UIKit

class WNTabLastView: UIView {
    
    static let shared = WNTabLastView()

    lazy var bookImage: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.wn_cornerWithBorder(2)
        return imageView
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        return label
    }()
    
    lazy var lastLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_999999")
        return label
    }()

    lazy var readBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("Continue", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 11)
        btn.rx.tap.bind { [weak self] in
            wn_getTopVC()?.wn_pushBookVC(self?.model?.bookId ?? "", "\(self?.model?.durChapterId ?? 0)")
            hidenTabLast()
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = .init(named: "red_FE95B4")
        btn.wn_cornerWithBorder(16)
        return btn
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "rate_close"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            hidenTabLast()
        }.disposed(by: rx.disposeBag)
        return btn
    }()

    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: NSLocalizedString("placeholder_banner", comment: ""))
                nameLabel.text = m.name
                lastLabel.text = "Last read \(m.durChapterName ?? "")"
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(named: "gray_ECE0E4")
        addSubview(bookImage)
        addSubview(closeBtn)
        addSubview(nameLabel)
        addSubview(lastLabel)
        addSubview(readBtn)
        bookImage.snp.makeConstraints { make in
            make.left.bottom.equalTo(0)
            make.width.equalTo(scaleWidth(48))
            make.height.equalTo(scaleWidth(68))
        }
        closeBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-6)
            make.size.equalTo(CGSize.init(width: 24, height: 24))
        }
        readBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(closeBtn.snp.left).offset(-6)
            make.size.equalTo(CGSize.init(width: 70, height: 32))
        }
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(bookImage.snp.right).offset(8)
            make.right.equalTo(readBtn.snp.left).offset(-6)
        }
        lastLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.right.equalTo(nameLabel.snp.right)
            make.bottom.equalTo(-8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

func showTabLast(_ vc: WNBaseVC, _ model: BookDetailModel?) {
    WNTabLastView.shared.frame = CGRect(x: 0, y: ScreenH-TabBarTotalHeight-60, width: ScreenW, height: 60)
    WNTabLastView.shared.model = model
    vc.view.addSubview(WNTabLastView.shared)
}

func hidenTabLast() {
    WNTabLastView.shared.removeFromSuperview()
}
